package com.shrct.businessprocess.project.impl;

import com.shrct.businessprocess.project.RCSalesCommissionYearConfig;
import com.shrct.businessprocess.project.RCSalesTeam;
import com.shrct.businessprocess.project.RCTeamMemberAssignment;
import com.shrct.businessprocess.project.impl.RCSalesCommissionYearConfigImpl;
import com.shrct.businessprocess.project.impl.RCTeamMemberAssignmentImpl;
import com.shrct.businessprocess.project.proxy.RCSalesTeamProxy;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCSALESTEAM_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcsalesteam")
public class RCSalesTeamImpl extends BaseModelObject implements RCSalesTeam {
  public RCSalesTeamImpl() {
    super();
  }
  
  public RCSalesTeamImpl(final RCSalesCommissionYearConfig config) {
    super();
    this.config = config;
  }
  
  @JoinColumn(name = "config_id")
  @ManyToOne(targetEntity = RCSalesCommissionYearConfigImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCSalesCommissionYearConfig config;
  
  public RCSalesCommissionYearConfig getConfig() {
    return this.config;
  }
  
  public RCSalesTeam setConfig(final RCSalesCommissionYearConfig config) {
    this.config = config;
    return this;			
    
  }
  
  private String name;
  
  public String getName() {
    return this.name;
  }
  
  public RCSalesTeam setName(final String name) {
    this.name = name;
    return this;			
    
  }
  
  private Double objectRate;
  
  public Double getObjectRate() {
    return this.objectRate;
  }
  
  public RCSalesTeam setObjectRate(final Double objectRate) {
    this.objectRate = objectRate;
    return this;			
    
  }
  
  private Double rate;
  
  public Double getRate() {
    return this.rate;
  }
  
  public RCSalesTeam setRate(final Double rate) {
    this.rate = rate;
    return this;			
    
  }
  
  public String getEntityName() {
    return this.name;
  }
  
  @OneToMany(targetEntity = RCTeamMemberAssignmentImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "team")
  private Collection<RCTeamMemberAssignment> members = new java.util.ArrayList<com.shrct.businessprocess.project.RCTeamMemberAssignment>();;
  
  public Collection<RCTeamMemberAssignment> getMembers() {
    return this.members;
  }
  
  public RCTeamMemberAssignment createAndAddtoMembers() {
    getMembers().size();
    com.shrct.businessprocess.project.RCTeamMemberAssignment rCTeamMemberAssignment = new com.shrct.businessprocess.project.impl.RCTeamMemberAssignmentImpl(this);
    getObjectFactory().create(rCTeamMemberAssignment);
    getMembers().add(rCTeamMemberAssignment);
    return rCTeamMemberAssignment;
    
  }
  
  public RCTeamMemberAssignment createAndAddtoMembers(final Procedure1<RCTeamMemberAssignment> updater) {
    getMembers().size();
    com.shrct.businessprocess.project.RCTeamMemberAssignment rCTeamMemberAssignment = new com.shrct.businessprocess.project.impl.RCTeamMemberAssignmentImpl(this);
    getObjectFactory().create(rCTeamMemberAssignment);
    getMembers().add(rCTeamMemberAssignment);
    return rCTeamMemberAssignment;
    
  }
  
  public RCSalesTeam removeAllMembers() {
    for(RCTeamMemberAssignment obj : this.members){
    	getObjectFactory().delete(obj);
    }
    this.members.clear();
    return this;	
    
  }
  
  public RCSalesTeam removeFromMembers(final RCTeamMemberAssignment rCTeamMemberAssignment) {
    this.members.remove(rCTeamMemberAssignment);
    getObjectFactory().delete(rCTeamMemberAssignment);
    return this;
    
  }
  
  public RCTeamMemberAssignment createMember(final Procedure1<? super RCTeamMemberAssignment> init) {
    RCTeamMemberAssignment _createAndAddtoMembers = this.createAndAddtoMembers();
    final Procedure1<RCTeamMemberAssignment> _function = new Procedure1<RCTeamMemberAssignment>() {
        public void apply(final RCTeamMemberAssignment it) {
          init.apply(it);
          it.checkValid();
        }
      };
    RCTeamMemberAssignment _doubleArrow = ObjectExtensions.<RCTeamMemberAssignment>operator_doubleArrow(_createAndAddtoMembers, _function);
    return _doubleArrow;
  }
  
  public void deleteMember(final RCTeamMemberAssignment item) {
    this.removeFromMembers(item);
  }
  
  /**
   * 团队毛利润指标实际完成率
   */
  public Double getActualFinishRate() {
    double _xblockexpression = (double) 0;
    {
      final Function1<RCTeamMemberAssignment,Double> _function = new Function1<RCTeamMemberAssignment,Double>() {
          public Double apply(final RCTeamMemberAssignment it) {
            Double _teamGrossProfitGoal = it.getTeamGrossProfitGoal();
            return _teamGrossProfitGoal;
          }
        };
      Iterable<Double> _map = IterableExtensions.<RCTeamMemberAssignment, Double>map(this.members, _function);
      final Function2<Double,Double,Double> _function_1 = new Function2<Double,Double,Double>() {
          public Double apply(final Double d1, final Double d2) {
            double _plus = DoubleExtensions.operator_plus(d1, d2);
            return Double.valueOf(_plus);
          }
        };
      Double _reduce = IterableExtensions.<Double>reduce(_map, _function_1);
      final Double allTeamGrossProfitGoal = NumberExtensions.nullsafe(_reduce);
      final Function1<RCTeamMemberAssignment,Double> _function_2 = new Function1<RCTeamMemberAssignment,Double>() {
          public Double apply(final RCTeamMemberAssignment it) {
            Double _contractProfit = it.getContractProfit();
            return _contractProfit;
          }
        };
      Iterable<Double> _map_1 = IterableExtensions.<RCTeamMemberAssignment, Double>map(this.members, _function_2);
      final Function2<Double,Double,Double> _function_3 = new Function2<Double,Double,Double>() {
          public Double apply(final Double d1, final Double d2) {
            double _plus = DoubleExtensions.operator_plus(d1, d2);
            return Double.valueOf(_plus);
          }
        };
      Double _reduce_1 = IterableExtensions.<Double>reduce(_map_1, _function_3);
      final Double allAgp = NumberExtensions.nullsafe(_reduce_1);
      double _divide = DoubleExtensions.operator_divide(allAgp, allTeamGrossProfitGoal);
      _xblockexpression = (_divide);
    }
    return Double.valueOf(_xblockexpression);
  }
  
  /**
   * 本年度应得管理奖
   */
  public Double getPrizeAmount() {
    double _xifexpression = (double) 0;
    Double _actualFinishRate = this.getActualFinishRate();
    boolean _lessThan = (_actualFinishRate.compareTo(this.objectRate) < 0);
    if (_lessThan) {
      _xifexpression = 0d;
    } else {
      final Function1<RCTeamMemberAssignment,Double> _function = new Function1<RCTeamMemberAssignment,Double>() {
          public Double apply(final RCTeamMemberAssignment it) {
            Double _teamContributeAmount = it.getTeamContributeAmount();
            return _teamContributeAmount;
          }
        };
      Iterable<Double> _map = IterableExtensions.<RCTeamMemberAssignment, Double>map(this.members, _function);
      final Function2<Double,Double,Double> _function_1 = new Function2<Double,Double,Double>() {
          public Double apply(final Double d1, final Double d2) {
            double _plus = DoubleExtensions.operator_plus(d1, d2);
            return Double.valueOf(_plus);
          }
        };
      Double _reduce = IterableExtensions.<Double>reduce(_map, _function_1);
      Double _nullsafe = NumberExtensions.nullsafe(_reduce);
      double _multiply = DoubleExtensions.operator_multiply(_nullsafe, this.rate);
      _xifexpression = _multiply;
    }
    return Double.valueOf(_xifexpression);
  }
  
  public Double getPrizeAmountTarget() {
    double _xifexpression = (double) 0;
    Double _actualFinishRate = this.getActualFinishRate();
    boolean _lessThan = (_actualFinishRate.compareTo(this.objectRate) < 0);
    if (_lessThan) {
      _xifexpression = 0d;
    } else {
      final Function1<RCTeamMemberAssignment,Double> _function = new Function1<RCTeamMemberAssignment,Double>() {
          public Double apply(final RCTeamMemberAssignment it) {
            Double _teamContributeAmountTarget = it.getTeamContributeAmountTarget();
            return _teamContributeAmountTarget;
          }
        };
      Iterable<Double> _map = IterableExtensions.<RCTeamMemberAssignment, Double>map(this.members, _function);
      final Function2<Double,Double,Double> _function_1 = new Function2<Double,Double,Double>() {
          public Double apply(final Double d1, final Double d2) {
            double _plus = DoubleExtensions.operator_plus(d1, d2);
            return Double.valueOf(_plus);
          }
        };
      Double _reduce = IterableExtensions.<Double>reduce(_map, _function_1);
      Double _nullsafe = NumberExtensions.nullsafe(_reduce);
      double _multiply = DoubleExtensions.operator_multiply(_nullsafe, this.rate);
      _xifexpression = _multiply;
    }
    return Double.valueOf(_xifexpression);
  }
  
  public static RCSalesTeam create(final RCSalesCommissionYearConfig config, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.project.RCSalesTeam rCSalesTeam = new com.shrct.businessprocess.project.impl.RCSalesTeamImpl(
    	config
    );
    objectFactory.create(rCSalesTeam);
    return rCSalesTeam;			
    
  }
  
  public static RCSalesTeam createRCSalesTeamImpl(final RCSalesCommissionYearConfig config, final IObjectFactory objectFactory, final Procedure1<RCSalesTeam> init) {
    com.shrct.businessprocess.project.RCSalesTeam rCSalesTeam = new com.shrct.businessprocess.project.impl.RCSalesTeamImpl(
    	config
    );
    objectFactory.create(rCSalesTeam);
    init.apply(rCSalesTeam);
    
    return rCSalesTeam;			
    
  }
  
  public RCSalesTeamProxy toProxy() {
    com.shrct.businessprocess.project.proxy.RCSalesTeamProxy proxy = new com.shrct.businessprocess.project.proxy.RCSalesTeamProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
